<template>
  <div>
    <el-form ref="editPram" :model="editPram" label-width="130px">
      <el-form-item
        label="名称"
        prop="name"
        :rules="[{ required:true,message:'请输入名称',trigger:['blur','change'] }]"
      >
        <el-input v-model="editPram.name" maxlength="150" placeholder="名称" />
      </el-form-item>
      <el-form-item label="父级">
        <el-cascader v-model="editPram.pid" :disabled="isCreate ===1 && editPram.pid ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
        <!-- allTreeList or parentOptions-->
      </el-form-item>
      <el-form-item label="是否多选" v-if="editPram.pid ===0">
        <el-switch v-model="editPram.choice"  active-text="是"
                   inactive-text="否" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="editPram.status"  active-text="显示"
                   inactive-text="隐藏" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="editPram.sort" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loadingBtn" @click="handlerSubmit('editPram')" v-hasPermi="[permissionNamePrefix+'save']">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<!--创建和编辑公用一个组件-->
<script>
import * as categoryApi from '@/api/categoryApi.js'
import * as roleApi from '@/api/role.js'
import * as selfUtil from '@/utils/ZBKJIutil.js'
import {addCategroyForDepartment} from "@/api/categoryApi.js";
export default {
  // name: "edit"
  props: {
    prent: {
      type: Object,
      required: true
    },
    isCreate: {
      type: Number,
      default: 0
    },
    biztype: {
      type: Object,
      required: true
    },
    editData: {
      type: Object
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    const validatePhone = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请填写手机号'));
        } else if (!/^1[3456789]\d{9}$/.test(value)) {
          callback(new Error('手机号格式不正确!'));
        } else {
          callback();
        }
      };

    return {
      isDisabled: this.$route.params.isDisabled==='1'?true:false,
      loadingBtn: false,
      constants: this.$constants,
      editPram: {
        name: null,
        shortName: null,
        pid: null,
        sort: 0,
        status: 1,
        //isCards: 0,
        choice:1,
        id: 0
      },
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
      parentOptions: [],
      permissionNamePrefix:'',
    }
  },
  beforeMount() {
    if(this.biztype.value == 1){
      //公用数据配置
      this.permissionNamePrefix='admin:system:category:'
    }else if(this.biztype.value == 2){
      //问卷设计
      //this.isShowTableLine1 = ['1']
      this.permissionNamePrefix='admin:question:design:'
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },

    initEditData() {
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')
      const { name, shortName, pid, sort, status, id, choice } = this.editData
      if(this.isCreate === 1){
        this.editPram.name = name
        this.editPram.shortName = shortName
        this.editPram.pid = pid
        this.editPram.sort = sort
        this.editPram.status = status
        //this.editPram.isCards = isCards
        this.editPram.choice = choice
        this.editPram.id = id
      }else{
        this.editPram.pid = this.prent.id
      }
    },
    //处理哪一级别禁用
    addTreeListLabelForCasCard(arr, child) {
      if(this.biztype.value === 2){
        arr.forEach((o,i) => {
          if (o.child && o.child.length) {
            // o.disabled = true
            o.child.forEach((j) => {
              j.disabled = true

              if (j.child && j.child.length) {
                j.child.forEach((k) => {
                  k.disabled = true
                })
              }
            })
          }
        })
      }else{
        arr.forEach((o,i) => {
          if (o.child && o.child.length) {
            // o.disabled = true
            o.child.forEach((j) => {
              j.disabled = true
            })
          }
        })
      }
    },
    handlerSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return
        this.handlerSaveOrUpdate(this.isCreate === 0)
      })
    },
    handlerSaveOrUpdate(isSave) {
      if (isSave) {
        //this.editPram.pid = this.prent.id //本身注释
        this.loadingBtn = true
        categoryApi.addCategroy(this.editPram,this.biztype.value).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('创建成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      } else {
        this.editPram.pid = Array.isArray(this.editPram.pid) ? this.editPram.pid[0] : this.editPram.pid
        this.loadingBtn = true
        categoryApi.addCategroy(this.editPram,this.biztype.value).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('更新成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
